unit AfterDownload_ConditionForm;

interface

uses
  AfterDownload_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Mask, JvExMask, JvToolEdit, 
  JvGroupHeader, JvBaseDlg, JvSelectDirectory, JvBaseEdits, JvExControls;

type
  TAD_ConditionForm = class(TForm)
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    CBX_Category: TComboBox;
    RB_Category: TRadioButton;
    RB_URL: TRadioButton;
    RB_Folder: TRadioButton;
    RB_Filename: TRadioButton;
    RB_SizeLT: TRadioButton;
    RB_SizeGT: TRadioButton;
    RB_SizeEQ: TRadioButton;
    FE: TJvFilenameEdit;
    RB_Flag: TRadioButton;
    RB_CategoryHas: TRadioButton;
    RB_Referer: TRadioButton;
    RB_Desc: TRadioButton;
    GH_Category: TJvGroupHeader;
    GH_Text: TJvGroupHeader;
    GH_Folder: TJvGroupHeader;
    GH_Size: TJvGroupHeader;
    GH_Special: TJvGroupHeader;
    CBX_Text: TComboBox;
    JSD_Folder: TJvSelectDirectory;
    CBX_Folder: TComboBox;
    SB_Folder: TSpeedButton;
    JCE_Size: TJvCalcEdit;
    SB_CompileRE: TSpeedButton;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBX_CategoryChange(Sender: TObject);
    procedure FEChange(Sender: TObject);
    procedure RB_CategoryClick(Sender: TObject);
    procedure RB_URLClick(Sender: TObject);
    procedure RB_FolderClick(Sender: TObject);
    procedure RB_FilenameClick(Sender: TObject);
    procedure RB_SizeLTClick(Sender: TObject);
    procedure RB_SizeGTClick(Sender: TObject);
    procedure RB_SizeEQClick(Sender: TObject);
    procedure RB_FlagClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure RB_CategoryHasClick(Sender: TObject);
    procedure CBX_TextChange(Sender: TObject);
    procedure RB_RefererClick(Sender: TObject);
    procedure RB_DescClick(Sender: TObject);
    procedure CBX_FolderChange(Sender: TObject);
    procedure SB_FolderClick(Sender: TObject);
    procedure JCE_SizeChange(Sender: TObject);
    procedure SB_CompileREClick(Sender: TObject);
  private
    { Private declarations }
    FCondition: Condition;
    procedure BitBtnClick(BitBtn: TBitBtn);
    procedure SetLanguage(language: String);
    procedure TranslateToo;
    procedure SetCondition(ACondition: Condition);
    procedure SetCategories;
    procedure CheckCondition(Other: Boolean = true);
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property myCondition: Condition read FCondition write SetCondition;
  end;

var
  AD_CF: TAD_ConditionForm = nil;

implementation

{$R *.dfm}

uses
 gnugettext,
 Locales,
 RegExpr,
 Tokens;

var
 CanSetCond: Boolean = true;

procedure TAD_ConditionForm.BitBtnClick(BitBtn: TBitBtn);
begin
 Self.ModalResult:=BitBtn.ModalResult;
 if not (fsModal in Self.FormState) then
   Self.Close;
end;

procedure TAD_ConditionForm.BitBtn1Click(Sender: TObject);
begin
 BitBtnClick(BitBtn1);
end;

procedure TAD_ConditionForm.BitBtn2Click(Sender: TObject);
begin
 BitBtnClick(BitBtn2);
end;

procedure TAD_ConditionForm.SetLanguage(language: String);
begin
 TranslateForm(Self, Language, TranslateToo);
end;

procedure TAD_ConditionForm.TranslateToo;
begin
 BitBtn2.Caption:=_('Cancel');
end;

procedure TAD_ConditionForm.SetCondition(ACondition: Condition);
begin
 FCondition.Assign(ACondition);
 //
 CanSetCond:=false;
 CheckCondition(false);
 CanSetCond:=true;
end;

procedure TAD_ConditionForm.SetCategories;
 var
  i: Integer;
begin
 CBX_Category.Items.Clear;
 for i:=0 to CatList.Count-1 do
   CBX_Category.Items.Add(CatList.ValueFromIndex[i]);
end;

procedure TAD_ConditionForm.FormCreate(Sender: TObject);
begin
 TranslateComponent(Self);
 //
 FCondition:=Condition.Create(nil);
 //
 SetCategories;
 //
 CBX_Text.Sorted:=true;
end;

procedure TAD_ConditionForm.CheckCondition(Other: Boolean = true);
 var
  i: Integer;
  s: String;
 procedure SetEdit(Edit: TEdit; AText: String; Clear: Boolean = false);
 begin
  if Clear then
    Edit.Text:=''
  else
    Edit.Text:=AText;
 end;
 procedure SetCBX(CBX: TComboBox; AText: String; Clear: Boolean = false);
  var
    Index: Integer;
 begin
  if Clear then
    CBX.Text:=''
  else
   begin
    Index:=CBX.Items.IndexOf(AText);
    if Index<0 then
      CBX.Text:=AText
    else
      CBX.ItemIndex:=Index;
    CBX.Hint:=CBX.Text;  
   end;
 end;
begin
 RB_Category.Checked:=FCondition.CondType=cndCategory;
 RB_CategoryHas.Checked:=FCondition.CondType=cndCategoryHas;
 CBX_Category.Enabled:=RB_Category.Checked or RB_CategoryHas.Checked;
 if CBX_Category.Enabled then
  begin
   if Other then
     i:=0
   else
    begin
     i:=IndexOfValue(FCondition.Parameter, CatList);
     if i<0 then
       i:=0;
    end;
   CBX_Category.ItemIndex:=i;
   if i=0 then
     FCondition.Parameter:=CBX_Category.Text;
  end
 else
   CBX_Category.ItemIndex:=0;
 //
 CBX_Text.Items.Clear;
 CBX_Text.Enabled:=FCondition.CondType in [cndURL, cndFilename, cndReferer, cndDescription];
 SB_CompileRE.Enabled:=CBX_Text.Enabled;
 if not CBX_Text.Enabled then
   SetCBX(CBX_Text, '');
 //
 RB_URL.Checked:=FCondition.CondType=cndURL;
 //ShowMessage(FCondition.Parameter); //debug
 if RB_URL.Checked then
  begin
   //build list of urls
   Assert(Assigned(myInfo), 'INFO must be assigned!');
   for i:=0 to myInfo.Count-1 do
    begin
     s:=ExtractToken('url', myInfo.ValueFromIndex[i]);
     if s<>'' then
      begin
       if CBX_Text.Items.IndexOf(s)<0 then
         CBX_Text.Items.Add(s);
      end;
    end;
   //
   SetCBX(CBX_Text, FCondition.Parameter, Other)
  end;
 //
 RB_Folder.Checked:=FCondition.CondType=cndFolder;
 CBX_Folder.Enabled:=RB_Folder.Checked;
 SB_Folder.Enabled:=RB_Folder.Checked;
 if CBX_Folder.Enabled then
  begin
   CBX_Folder.Items.Clear;
   //build list of folders
   Assert(Assigned(myInfo), 'INFO must be assigned!');
   for i:=0 to myInfo.Count-1 do
    begin
     s:=IncludeTrailingPathDelimiter(ExtractFilePath(ExtractToken('saveto', myInfo.ValueFromIndex[i])));
     if s<>'' then
      begin
       if CBX_Folder.Items.IndexOf(s)<0 then
         CBX_Folder.Items.Add(s);
      end;
    end;
   //
   SetCBX(CBX_Folder, FCondition.Parameter, Other)
  end
 else
   SetCBX(CBX_Folder, '');
 //
 RB_Filename.Checked:=FCondition.CondType=cndFilename;
 if RB_Filename.Checked then
  begin
   //build list of filenames
   Assert(Assigned(myInfo), 'INFO must be assigned!');
   for i:=0 to myInfo.Count-1 do
    begin
     s:=ExtractFileName(ExtractToken('saveto', myInfo.ValueFromIndex[i]));
     if s<>'' then
      begin
       if CBX_Text.Items.IndexOf(s)<0 then
         CBX_Text.Items.Add(s);
      end;
    end;
   //
   SetCBX(CBX_Text, FCondition.Parameter, Other)
  end;
 //
 RB_Referer.Checked:=FCondition.CondType=cndReferer;
 if RB_Referer.Checked then
  begin
   //build list of referers
   Assert(Assigned(myInfo), 'INFO must be assigned!');
   for i:=0 to myInfo.Count-1 do
    begin
     s:=ExtractToken('referer', myInfo.ValueFromIndex[i]);
     if s<>'' then
      begin
       if CBX_Text.Items.IndexOf(s)<0 then
         CBX_Text.Items.Add(s);
      end;
    end;
   //
   SetCBX(CBX_Text, FCondition.Parameter, Other)
  end;
 //
 RB_Desc.Checked:=FCondition.CondType=cndDescription;
 if RB_Desc.Checked then
  begin
   //build list of descriptions
   Assert(Assigned(myInfo), 'INFO must be assigned!');
   for i:=0 to myInfo.Count-1 do
    begin
     s:=ExtractToken('description', myInfo.ValueFromIndex[i]);
     if s<>'' then
      begin
       if CBX_Text.Items.IndexOf(s)<0 then
         CBX_Text.Items.Add(s);
      end;
    end;
   //
   SetCBX(CBX_Text, FCondition.Parameter, Other)
  end;
 //
 RB_SizeLT.Checked:=FCondition.CondType=cndSizeLT;
 //RB_SizeLE.Checked:=FCondition.CondType=cndSizeLE;
 RB_SizeGT.Checked:=FCondition.CondType=cndSizeGT;
 //RB_SizeGE.Checked:=FCondition.CondType=cndSizeGE;
 RB_SizeEQ.Checked:=FCondition.CondType=cndSizeEQ;
 JCE_Size.Enabled:=FCondition.CondType in [cndSizeLT, cndSizeGT, cndSizeEQ];
 if JCE_Size.Enabled then
   JCE_Size.Value:=StrToIntDef(FCondition.Parameter, 0)
 else
   JCE_Size.Value:=0;
 //
 RB_Flag.Checked:=FCondition.CondType=cndFlagExists;
 //RB_FlagNo.Checked:=FCondition.CondType=cndFlagDontExists;
 FE.Enabled:=RB_Flag.Checked{ or RB_FlagNo.Checked};
 if FE.Enabled then
  begin
   if Other then
     FE.Text:=''
   else
     FE.Text:=FCondition.Parameter;
  end
 else
   FE.Text:='';
end;

procedure TAD_ConditionForm.CBX_CategoryChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=CBX_Category.Text;
end;

procedure TAD_ConditionForm.FEChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=FE.Text;
end;

procedure TAD_ConditionForm.RB_CategoryClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndCategory;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_URLClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndURL;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_FolderClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFolder;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_FilenameClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFilename;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_SizeLTClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeLT;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_SizeGTClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeGT;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_SizeEQClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeEQ;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_FlagClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFlagExists;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
 var
  //re: TRegExpr;
  //i: Integer;
  s: String;
begin
 if ModalResult=mrOK then
  begin
   if FCondition.CondType=0 then
    begin
     MessageDlg(_('Please select type of condition!'), mtError, [mbOK], 0);
     CanClose:=false;
     Exit;
    end;
   if CBX_Text.Enabled then
    begin
     {
     re:=TRegExpr.Create;
     re.ModifierI:=true; //case insensitive search
     re.Expression:=CBX_Text.Text;
     try
      CanClose:=true;
      re.Compile;
     except
      CanClose:=false;
      ActiveControl:=CBX_Text;
      raise;
     end;
     FreeAndNil(re);
     }
     s:=CompileRE(CBX_Text.Text);
     if s<>'' then
      begin
       CanClose:=False;
       ActiveControl:=CBX_Text;
       MessageDlg(Format(_('Error in regular expression: %s'), [s]), mtError, [mbOK], 0);
      end;
     Exit;
    end;
   if JCE_Size.Enabled then
    begin
     if JCE_Size.AsInteger<0 then
      begin
       MessageDlg(_('Size value must be 0 or greater!'), mtError, [mbOK], 0);
       ActiveControl:=JCE_Size;
       CanClose:=false;
       Exit;
      end;
    end;
   //set external function flag
   FCondition.ExternalFlag:=FCondition.CondType=cndCategoryHas;
  end;
end;

procedure TAD_ConditionForm.FormDestroy(Sender: TObject);
begin
 FreeAndNil(FCondition);
end;

procedure TAD_ConditionForm.RB_CategoryHasClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndCategoryHas;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.CBX_TextChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=CBX_Text.Text;
end;

procedure TAD_ConditionForm.RB_RefererClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndReferer;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.RB_DescClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndDescription;
   CheckCondition();
  end;
end;

procedure TAD_ConditionForm.CBX_FolderChange(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.Parameter:=IncludeTrailingPathDelimiter(CBX_Folder.Text);
   CBX_Folder.Hint:=CBX_Folder.Text;
  end;
end;

procedure TAD_ConditionForm.SB_FolderClick(Sender: TObject);
begin
 //JSD_Folder.InitialDir:=CBX_Folder.Text;
 if JSD_Folder.Execute then
   CBX_Folder.Text:=JSD_Folder.Directory;
end;

procedure TAD_ConditionForm.JCE_SizeChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=IntToStr(JCE_Size.AsInteger);
end;

procedure TAD_ConditionForm.SB_CompileREClick(Sender: TObject);
 var
   s: string;
begin
 s:=CompileRE(CBX_Text.Text);
 if s='' then
   MessageDlg(Format(_('Regular expression is valid.'), [s]), mtInformation, [mbOK], 0)
 else
   MessageDlg(Format(_('Error in regular expression: %s'), [s]), mtError, [mbOK], 0);
end;

end.
